﻿using System;
using System.Web.UI.WebControls;
using Bingosoft.Modules.SecurityConsole.Organization;
using Bingosoft.Web.Utils;

namespace Bingosoft.WebApp.Modules.SecurityConsole.Organization
{
    public partial class OrganizationEdit : System.Web.UI.BasePage
    {
        private OrganizationService _organizationService;
        private OrganizationService OrganizationService
        {
            get
            {
                return _organizationService ??
                       (_organizationService = ObjectHelper.GetObject<OrganizationService>());
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化页面元素
            if (!this.IsPostBack && FormViewMode.Insert.Equals(this.SmartFormView1.CurrentMode))
            {
                this.SmartFormView1["Parent"] = (String.IsNullOrEmpty(Request["Parent"]) ? "" : Request["Parent"]);
                this.SmartFormView1["ParentName"] = (String.IsNullOrEmpty(Request["ParentName"])?"根组织":Request["ParentName"]);
            }
        }

        protected Object GetParentName(Object obj)
        {
            if (obj is String && String.IsNullOrEmpty(obj as String))
            {
                return "根组织";
            }

            if (obj is DBNull)
            {
                return "根组织";
            }

            return obj;
        }

        protected void SaveOrganization(object sender, EventArgs e)
        {
            //从Form表单获取值
            var orgInfo = this.SmartFormView1.ToEntity<OrganizationInfo>();

            //保存
            this.OrganizationService.SaveOrUpdate(orgInfo);

            ScriptHelper.CloseDialog(this, "true");                       
        }
    }
}